package com.ce.webframework.sec.util;

import java.util.Date;
import java.util.HashMap;

import javax.servlet.ServletContext;

import com.ce.webframework.sec.vo.MsgGlobal;
import com.ce.webframework.sec.vo.SecMsgTipsVO;

public class CommonUtil {

	public static String DAILY_TIMERS = "dailyTimers";
	public static String MSG_GLOBAL = "msgGlobal";

	/**
	 * 功能描述:删除消息定时器
	 * @param servletContext
	 * @param id
	 */
	public static void removeMsgTimer(ServletContext servletContext, Long id){
		HashMap<String, TimerTask> map = (HashMap<String, TimerTask>)servletContext.getAttribute(DAILY_TIMERS);
		map.remove(id);
	}

	/**
	 * 功能描述:增加消息定时器
	 * @param servletContext
	 */
	public static void addMsgTimer(ServletContext servletContext, Object obj, String methodName, SecMsgTipsVO secMsgTipsVO){
		HashMap<String, TimerTask> map = (HashMap<String, TimerTask>)servletContext.getAttribute(DAILY_TIMERS);
		if(map == null){
			map = new HashMap<String, TimerTask>();
			servletContext.setAttribute(DAILY_TIMERS, map);
		}
		Object[] objs = new Object[1];
		objs[0] = secMsgTipsVO;
		TimerTask tt = new TimerTask(obj, methodName, objs);
		tt.start(TimerTask.NOT_REPEAT, secMsgTipsVO.getRemindTime(), null);
		map.put(secMsgTipsVO.getMsgTipId().toString(), tt);

	}

	/**
	 * 功能描述:增加用户编码到全局对象MsgGlobal
	 * @param servletContext
	 * @param usercode
	 */
	public static void addMsgGlobal(ServletContext servletContext, String usercode){
		MsgGlobal mg =(MsgGlobal) servletContext.getAttribute(MSG_GLOBAL);
		mg.getUserCodeList().add(usercode);
	}

	/**
	 * String数组转化为Long数组
	 * @param source
	 * @return
	 */
	public static Long[] arrayStringToLong(String[] source){
		Long[] target = new Long[source.length];
		for(int i=0;i<source.length;i++){
			target[i] = Long.valueOf(source[i]);
		}
		return target;
	}
}
